<template>
  <div class="login">
    <div class="box">
      <!-- 头像 -->
      <div class="avatar">
        <img src="../assets/logo.png" alt=""/>
      </div>
      <!-- 表单 -->
      <el-form ref="form" :model="form" :rules="rules" class="form" label-width="0px">
        <!-- 用户名 -->
        <el-form-item prop="username"> <!-- prop 与校验 rules 配合使用 -->
          <el-input
            v-model="form.username"
            prefix-icon="iconfont icon-user"
            placeholder="用户名"
          />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="iconfont icon-3702mima"
            placeholder="密码"
            type="password"
          />
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btn-group">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetFields">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetFields () {
      this.$refs.form.resetFields()
    },
    login () {
      this.$refs.form.validate(async valid => {
        // 表单合法性验证
        if (!valid) return
        try {
          // 向后台请求登录
          const { data } = await this.$http.post('login', this.form)
          if (data.meta.status !== 200) {
            return this.$message.error('登录失败，' + data.meta.msg)
          }
          // 保存返回的 token
          window.sessionStorage.setItem('token', data.data.token)
          // 路由跳转
          this.$router.push('/home')
        } catch (e) {
          return this.$message.error('网络异常')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  background: #2b4b6b;
  color: white;
  height: 100%;
  /* 居中 */
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 3px;
    position: relative;
    .avatar {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      background: #fff;
      /* 顶部居中（结合外层的 relative）*/
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: #eee;
      }
    }
    .form {
      margin: 100px 20px 0 20px;
      .btn-group {
        text-align: right;
      }
    }
  }
}
</style>
